  'use strict';
  import Base from './base.js';
  //美易通系统类控制器请求方法
  module.exports = class extends Base {
  //请求登录
	async getloginAction(){
		let orcode = this.get('orcode');
		let username = this.get('username');
		let password = think.md5(this.get('password'));
		let info = await this.model().query(' SELECT au.id,au.name,au.sex,od.orcode,org.jsonshop,org.modelarr,od.floor,au.departmentid,od.name as departmentname,au.ismanage FROM `think_or_app_user` au LEFT JOIN `think_or_department` od ON au.departmentid = od.id LEFT JOIN `think_or_rulegroup` org ON au.zhichengid = org.groupid WHERE od.orcode = '+orcode+' AND au.username = "'+username+'" AND au.password = "'+password+'" ');
		if (think.isEmpty(info)) {
			return this.json({'code':"200",'msg':"账号不存在...",'data':info});
		} else {
			if (think.isEmpty(info[0].modelarr)) {
				info[0].modelarr = 0
			}
			return this.json({'code':"100",'msg':"登录成功...",'data':info});
		}
	}
  //请求获取系统机构信息
  async getsyorcodesysteminfoAction(){
    let orcode = this.get('orcode');
    //机构名称
    let info = await this.model().query(' SELECT name,orcode FROM `think_or_department` WHERE orcode = '+orcode+' AND floor = 0 ');
    //机构下分店数量
    let shopnum = await this.model().query(' SELECT IFNULL(count(id),0) as shopnum FROM `think_or_department` WHERE orcode = '+orcode+' AND floor = 1 ');
    //机构下所有员工的数量
    let workernum = await this.model().query(' SELECT IFNULL(count(au.id),0) as workernum FROM `think_or_app_user` au LEFT JOIN `think_or_department` od ON au.departmentid = od.id WHERE od.orcode = '+orcode+' ');
    //机构下客户数量
    let guestnum = await this.model().query(' SELECT IFNULL(count(og.id),0) as guestnum FROM `think_or_guest` og LEFT JOIN `think_or_department` od ON og.departmentid = od.id WHERE od.orcode = '+orcode+' ');
    //机构下订单数量
    let ordernum = await this.model().query(' SELECT IFNULL(count(oso.id),0) as ordernum FROM `think_or_shop_order` oso LEFT JOIN `think_or_department` od ON oso.departmentid = od.id WHERE od.orcode = '+orcode+' ');
    //机构下的订单总金额
    let amount = await this.model().query(' SELECT IFNULL(sum(oso.recemoney),0) as amount FROM `think_or_shop_order` oso LEFT JOIN `think_or_department` od ON oso.departmentid = od.id WHERE od.orcode = '+orcode+' AND oso.status <> 0 ');
    //机构的系统开通时间和结束时间
    let time = await this.model().query(' SELECT odi.begintime,odi.endtime FROM `think_or_department` od LEFT JOIN `think_or_department_info` odi ON od.id = odi.departmentid WHERE od.orcode = '+orcode+' AND od.floor = 0 ');

    return think.isEmpty(info)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'base':info,'shopnum':shopnum,'workernum':workernum,'guestnum':guestnum,'ordernum':ordernum,'amount':amount,'time':time});

  }
  //请求获取机构人员管理店的数量及信息
  async getmyjsonshoplistAction(){
    let jsonshop = this.get('jsonshop');
    let info = await this.model().query(' SELECT id,name FROM `think_or_department` WHERE id in ('+jsonshop+') ');
    return think.isEmpty(info)?this.json({'code':"200",'msg':"请设置你的管理权限！",'data':''}):this.json({'code':"100",'msg':"查找成功...",'data':info});
  }
  //请求获取首页动态的图表数据
  async getsystemdongtaiAction(){
   let orcode = this.get('orcode');
   let jsonshop = this.get('jsonshop');
   let floor = this.get('floor');
   let ismanage = this.get('ismanage');
   let user_id = this.get('user_id');
   //获取本月开始的时间戳
   var starttime =  Math.round(new Date(new Date().getFullYear(),new Date().getMonth(), 1).getTime()/1000);
   //本月结束时间
   var endtime = Math.round(new Date(new Date().getFullYear(),new Date().getMonth()+1, 1).getTime()/1000);
   switch(floor){
     //如果是总部运营和店长
     case '0':
     case '1':
     var chartsinfo = await this.model().query('SELECT IFNULL(sum(xianjinyeji),0) as xianjinyeji,IFNULL(sum(kahaoyeji),0) as kahaoyeji,day(FROM_UNIXTIME(addtime)) as day FROM `think_or_shop_yejiticheng` WHERE addtime >= '+starttime+' AND addtime < '+endtime+' AND shopid IN ('+jsonshop+') GROUP BY day ');
     break;
     //如果是经理
     case '2':
     var chartsinfo = await this.model().query('SELECT IFNULL(sum(xianjinyeji),0) as xianjinyeji,IFNULL(sum(kahaoyeji),0) as kahaoyeji,day(FROM_UNIXTIME(addtime)) as day FROM `think_or_shop_yejiticheng` WHERE addtime >= '+starttime+' AND addtime < '+endtime+' AND bumenid = '+jsonshop+' GROUP BY day ');
     break;
     //如果是小组
     case '3':
     if(ismanage == 1){//如果是管理层
      var chartsinfo = await this.model().query('SELECT IFNULL(sum(xianjinyeji),0) as xianjinyeji,IFNULL(sum(kahaoyeji),0) as kahaoyeji,day(FROM_UNIXTIME(addtime)) as day FROM `think_or_shop_yejiticheng` WHERE addtime >= '+starttime+' AND addtime < '+endtime+' AND xiaozuid = '+jsonshop+' GROUP BY day ');
     }else{//如果是普通员工
      var chartsinfo = await this.model().query('SELECT IFNULL(sum(xianjinyeji),0) as xianjinyeji,IFNULL(sum(kahaoyeji),0) as kahaoyeji,day(FROM_UNIXTIME(addtime)) as day FROM `think_or_shop_yejiticheng` WHERE addtime >= '+starttime+' AND addtime < '+endtime+' AND userid = '+user_id+' GROUP BY day ')
     }
     break;
   }


   return this.json({'code':"100",'msg':"查找成功...",'data':chartsinfo});
  }
  //请求获取首页动态展示数据
  async getindexdongtaiAction(){
    let user_id = this.get('user_id');
    let jsonshop = this.get('jsonshop');
    let floor = this.get('floor');
    let ismanage = this.get('ismanage');
    let departmentid = this.get('departmentid');
    //获取今日开始的时间戳
    let starttime = Math.round(new Date(new Date().toDateString()).getTime()/1000);
    //今天结束时间
    let endtime = starttime + 86400;
    //定义api获取地址
    const datapath = think.config('datapath');
    let getapi = this.controller(datapath+'/database');
    if(floor == 3 && ismanage == 0){//如果是普通员工
      let basenum = await getapi.getguestplannum(starttime,endtime,floor,jsonshop,ismanage,user_id);//规划客数
      let comenum = await getapi.getguestcomenum(starttime,endtime,floor,jsonshop,ismanage,user_id);//到店客数
      let bespeaknum = await getapi.getguestbesknum(starttime,endtime,floor,jsonshop,ismanage,user_id);//预约客户数量
      let bespeakarrive = await getapi.getguestbeskarrivenum(starttime,endtime,floor,jsonshop,ismanage,user_id);//预约到店数量
      let planfeat = await getapi.getyejiplanxianjin(starttime,endtime,floor,jsonshop,ismanage,user_id);//规划现金业绩
      let xianjin = await getapi.getyejixianjin(starttime,endtime,floor,jsonshop,ismanage,user_id);//实际现金业绩
      let kahao = await getapi.getyejikahao(starttime,endtime,floor,jsonshop,ismanage,user_id);//卡耗业绩

      return this.json({'data':{'basenum':basenum,'comenum':comenum,'bespeaknum':bespeaknum,'bespeakarrive':bespeakarrive,'planfeat':planfeat,'xianjin':xianjin,'kahao':kahao}});
    }else{//如果不是普通员工
        let allworknum = await getapi.getworkermanagenum(floor,jsonshop,ismanage,departmentid);//管理员工总数
        let worknum = await getapi.getworkerworkonnum(starttime,endtime,floor,jsonshop,ismanage,departmentid);//出勤员工数量
        let ablenum = await getapi.getworkersalenum(starttime,endtime,floor,jsonshop,ismanage,departmentid);//出单员工数量
        let basenum = await getapi.getguestplannum(starttime,endtime,floor,jsonshop,ismanage,user_id);//规划客数
        let comenum = await getapi.getguestcomenum(starttime,endtime,floor,jsonshop,ismanage,user_id);//到店客数
        let bespeaknum = await getapi.getguestbesknum(starttime,endtime,floor,jsonshop,ismanage,user_id);//预约客户数量
        let bespeakarrive = await getapi.getguestbeskarrivenum(starttime,endtime,floor,jsonshop,ismanage,user_id);//预约到店数量
        let planfeat = await getapi.getyejiplanxianjin(starttime,endtime,floor,jsonshop,ismanage,user_id);//规划现金业绩
        let xianjin = await getapi.getyejixianjin(starttime,endtime,floor,jsonshop,ismanage,user_id);//实际现金业绩
        let kahao = await getapi.getyejikahao(starttime,endtime,floor,jsonshop,ismanage,user_id);//卡耗业绩

      return this.json({'data':{'allworknum':allworknum,'worknum':worknum,'ablenum':ablenum,'basenum':basenum,'comenum':comenum,'bespeaknum':bespeaknum,'bespeakarrive':bespeakarrive,'planfeat':planfeat,'xianjin':xianjin,'kahao':kahao}});

    }

  }
  //请求获取机构的订单捷报列表
  async getorcodeorderlistAction(){
   let orcode = this.get('orcode');
   let page = this.get('page');
   let size = this.get('size');
   let nowpage = page * size;
   let res = await this.model().query('SELECT osy.userid,au.name,au.sex,au.headurl,od.name as shopname,IFNULL(osy.xianjinyeji,0) as xianjinyeji,osy.addtime,IFNULL(osy.kahaoyeji,0) as kahaoyeji FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id LEFT JOIN `think_or_department` od ON au.shopid = od.id WHERE od.orcode = '+orcode+' ORDER BY osy.addtime DESC LIMIT '+nowpage+','+size+' ');
   for(var i = 0 ; i < res.length; i++){
    let starttime = Math.round(new Date(new Date(res[i].addtime*1000).toDateString()).getTime()/1000);
    let num = await this.model().query('SELECT COUNT(id) as num FROM `think_or_shop_yejiticheng` WHERE addtime >= '+starttime+' AND addtime < '+res[i].addtime+' AND userid = '+res[i].userid+' ');
    res[i].ordernum = num[0].num+1;
   } 

   return think.isEmpty(res)?this.json({'code':"200",'msg':"查找失败...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'orderlist':res});
  }
  //请求工作页面top信息记录数量
  async getsystemworkinfoAction(){
    let jsonshop = this.get('jsonshop');
    let floor = this.get('floor');
    let ismanage = this.get('ismanage');
    let user_id = this.get('user_id');
    //定义api获取地址
    const datapath = think.config('datapath');
    let getapi = this.controller(datapath+'/database');
    //30天前的当天开始时间
    let starttime_30 = Math.round(new Date(new Date().toDateString()).getTime()/1000) - 86400*30;
    let nowtime = Math.round(Date.now()/1000);
    //当前年、月、日
    let year = new Date().getFullYear();    //获取年份
    let month = new Date().getMonth()+1;    //获取月份
    let day = new Date().getDate();    //获取日期
    //获取今日开始的时间戳
    let starttime = Math.round(new Date(new Date().toDateString()).getTime()/1000);
    //今天结束时间
    let endtime = starttime + 86400;
    
    let fuwunum = await getapi.getworkfuwunum(starttime_30,nowtime,floor,jsonshop,ismanage,user_id);//未完成的服务记录
    let huifangnum = await getapi.getworkhuifangnum(starttime_30,nowtime,floor,jsonshop,ismanage,user_id);//未完成的服务记录
    let daynum = await getapi.getworkermynomanageday(year,month,day,user_id);//我的未完成的日程数量
    let sheettype = await getapi.getworkertodayisdaysheet(starttime,endtime,user_id);//我今日的报表情况
    return this.json({'code':"100",'msg':"查找成功...",'fuwunum':fuwunum,'huifangnum':huifangnum,'daynum':daynum,'sheettype':sheettype});

  }
  //请求获取app上的所有权限列表
  async getappallauthorityAction(){
   let res = await this.model().query('SELECT * FROM `think_or_rulemodel` WHERE category = 1 or category = 2 ');
   return think.isEmpty(res)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':res});
  }
  //请求刷新用户权限
  async getuserauthorityAction(){
   let user_id = this.get('user_id');
   let authority = await this.model().query('SELECT org.modelarr FROM `think_or_app_user` au LEFT JOIN `think_or_rulegroup` org ON au.zhichengid = org.groupid WHERE au.id = '+user_id+'  '); 
   return think.isEmpty(authority)?this.json({'code':"200",'msg':"没有数据...",'data':''}):this.json({'code':"100",'msg':"查询成功...",'data':authority});
  }
  //请求提交并修改密码
  async postchangepwdAction(){
    let user_id = this.post('user_id');
    let nowpassword = think.md5(this.post('nowpassword'));
    let password = think.md5(this.post('password'));
    let info = await this.model().query('SELECT password FROM `think_or_app_user` WHERE id = '+user_id+' ');
    //console.log(password)
    if(info[0].password == nowpassword){
      let res = await this.model('or_app_user').where({id:user_id}).update({password:password});
      return think.isEmpty(res)?this.json({'code':"200",'msg':"修改失败...",'data':''}):this.json({'code':"100",'msg':"修改成功...",'data':''});
    }else{
      return this.json({'code':"200",'msg':"现在的密码不正确！...",'data':''})
    }

    
  }
  //请求用户提交设置出勤状态
  async postonworkAction(){
    let user_id = this.post('user_id');
    let type = this.post('type');
    let time_start = Math.round(Date.now()/1000);
    let data = {};
    data['userid'] = user_id;
    data['type'] = type;
    data['time_start'] = time_start;
    //获取今日开始的时间戳
    let starttime = Math.round(new Date(new Date().toDateString()).getTime()/1000);
    //今天结束时间
    let endtime = starttime + 86400;
    //查找今天是否已经提交了考勤
    let info = await this.model().query(' SELECT id FROM `think_or_user_work` WHERE userid = '+user_id+' AND time_start >= '+starttime+' AND time_start < '+endtime+' ');
    if(think.isEmpty(info)){
     //提交数据到数据库
    let res = await this.model('or_user_work').add(data);
    //返回结果数据
    return think.isEmpty(res)?this.json({'code':"200",'msg':"执行失败...",'data':''}):this.json({'code':"100",'msg':"success...",'data':res});
    }else{
    return this.json({'code':"200",'msg':"不要重复出勤...",'data':''})
    }

    
  }
  //请求用户结束出勤状态
  async postendworkAction(){
    let id = this.post('id');
    let type = this.post('type');
    let time_end = Math.round(Date.now()/1000);
    let data = {};
    data['type'] = type;
    data['time_end'] = time_end;
    //提交数据到数据库
    let res = await this.model('or_user_work').where({id:id}).update(data);
    //返回结果数据
    return think.isEmpty(res)?this.json({'code':"200",'msg':"执行失败...",'data':''}):this.json({'code':"100",'msg':"success...",'data':res});
  }
  //请求获取店内产品列表
  async getshopgoodslistAction(){
    let user_id = this.get('user_id');
    //获取该店的id
    let shopid = await this.model('or_app_user').where({id:user_id}).field('shopid').find();
    let res = await this.model().query(' SELECT ob.id as brandid,ob.name as brandname,og.name as goodsname,osg.id as goodsid,osg.price FROM `think_or_shop_goods` osg LEFT JOIN `think_or_goods` og ON osg.goodsid = og.id LEFT JOIN `think_or_brand` ob ON og.brandid = ob.id WHERE osg.shopid = '+shopid.shopid+' ');

    return think.isEmpty(res)?this.json({'code':"200",'msg':"执行失败...",'data':''}):this.json({'code':"100",'msg':"success...",'data':res});
  }
  //请求获取店内项目列表
  async getshopsubjectlistAction(){
    let user_id = this.get('user_id');
    //获取该店的id
    let shopid = await this.model('or_app_user').where({id:user_id}).field('shopid').find();
    let res = await this.model().query('SELECT osc.id as brandid,osc.name as brandname,os.name as goodsname,oss.id as goodsid,oss.price FROM `think_or_shop_subject` oss LEFT JOIN `think_or_subject` os ON oss.subjectid = os.id LEFT JOIN `think_or_subjectcategory` osc ON os.categoryid = osc.id  WHERE oss.shopid = '+shopid.shopid+' ');
    return think.isEmpty(res)?this.json({'code':"200",'msg':"执行失败...",'data':''}):this.json({'code':"100",'msg':"success...",'data':res});
  }
  //获取机构里的会员诊断设置信息
  async getadmindiagnosedlistAction(){
    let orcode = this.get('orcode');
    let guest_id = this.get('guest_id');
    let type = this.get('type');
    let info = await this.model().query(' SELECT odsc.id as topid,odsc.name as topname,ods.id as listid,ods.name as listname,odsc.id FROM `think_or_diagnosis` ods LEFT JOIN `think_or_diagnosiscategory` odsc ON ods.categoryid = odsc.id WHERE odsc.orcode = '+orcode+' AND odsc.pid = '+type+' ');
    let info1 = await this.model().query(' SELECT categoryid,status FROM `think_or_shop_guest_zengduan` WHERE guestid = '+guest_id+' ');
    return think.isEmpty(info)?this.json({'code':"200",'msg':"点赞失败...",'data':''}):this.json({'code':"100",'msg':"点赞成功+1...",'data':info,'selected':info1});
  }
  //设置会员诊断信息
  async postguestzengduanAction(){
    let guest_id = this.post('guest_id');
    let categoryid = this.post('categoryid');
    let info = await this.model().query('SELECT id,status FROM `think_or_shop_guest_zengduan` WHERE categoryid = '+categoryid+' AND guestid = '+guest_id+' ');

    if(think.isEmpty(info)){
      let info1 = await this.model('or_shop_guest_zengduan').add({guestid:guest_id,categoryid:categoryid,status:1});
      return think.isEmpty(info1)?this.json({'code':"200",'msg':"修改失败...",'data':''}):this.json({'code':"100",'msg':"修改成功...",'data':1});
    }else{
        if(info[0].status == 1){
          let info1 = await this.model('or_shop_guest_zengduan').where({id:info[0].id}).update({status:0});
          return think.isEmpty(info1)?this.json({'code':"200",'msg':"修改失败...",'data':''}):this.json({'code':"100",'msg':"修改成功...",'data':2});
        }else{
          let info1 = await this.model('or_shop_guest_zengduan').where({id:info[0].id}).update({status:1});
          return think.isEmpty(info1)?this.json({'code':"200",'msg':"修改失败...",'data':''}):this.json({'code':"100",'msg':"修改成功...",'data':3});
        }
    }
  }

  
  //bottom
};

